<script lang="ts">
export default {
  name: "devicelog",
};
</script>

<script setup lang="ts">
import { getDeviceList, getDeviceLogPage } from "@/api/device/log";

import {
  DeviceLogPageVO,
  DeviceLogQuery,
  DeviceListVO,
} from "@/api/device/logtypes";

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);

const queryParams = reactive<DeviceLogQuery>({
  pageNum: 1,
  pageSize: 10,
	deviceId: undefined,
	type: undefined

});

const deviceLogList = ref<DeviceLogPageVO[]>();

const deviceList = ref<DeviceListVO[]>();

/**
 * 查询设备
 */
function getDevice() {
  loading.value = true;
  getDeviceList()
    .then(({ data }) => {
      deviceList.value = data;
    })
    .finally(() => {
      loading.value = false;
    });
}

/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  getDeviceLogPage(queryParams)
    .then(({ data }) => {
      deviceLogList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

onMounted(() => {
  getDevice();
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="设备名称：">
          <el-select
            v-model="queryParams.deviceId"
            placeholder="请选择设备名称"
            prop="deviceId"
          >
            <el-option
              v-for="item in deviceList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备动作：">
          <el-select
            v-model="queryParams.type"
            placeholder="请选择设备动作"
            prop="type"
          >
            <el-option label="上线" value="1">上线</el-option>
            <el-option label="下线" value="2">下线</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header> </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="deviceLogList"
        highlight-current-row
        border
      >
        <el-table-column label="设备名称" prop="deviceName" />
        <el-table-column label="动作" prop="type" />
        <el-table-column label="说明" prop="memo" />
        <el-table-column label="记录时间" prop="time" />
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>
